package UML_ER.diagram.part;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

import org.eclipse.gmf.tooling.runtime.structure.DiagramStructure;
import UML_ER.BinaryRelationship;
import UML_ER.ERmodel;
import UML_ER.Relationship;
import UML_ER.Role;
import UML_ER.UML_ERPackage;
import UML_ER.diagram.edit.parts.AggregationEditPart;
import UML_ER.diagram.edit.parts.AggregationName2EditPart;
import UML_ER.diagram.edit.parts.AggregationNameEditPart;
import UML_ER.diagram.edit.parts.Attribute2EditPart;
import UML_ER.diagram.edit.parts.AttributeEditPart;
import UML_ER.diagram.edit.parts.BinaryRelationship2EditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipAttributesEditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipEditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipName2EditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipName3EditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipName4EditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipName5EditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipName6EditPart;
import UML_ER.diagram.edit.parts.BinaryRelationshipNameEditPart;
import UML_ER.diagram.edit.parts.CompositionEditPart;
import UML_ER.diagram.edit.parts.CompositionName2EditPart;
import UML_ER.diagram.edit.parts.CompositionNameEditPart;
import UML_ER.diagram.edit.parts.ERmodelEditPart;
import UML_ER.diagram.edit.parts.EntityAttributes2EditPart;
import UML_ER.diagram.edit.parts.EntityAttributesEditPart;
import UML_ER.diagram.edit.parts.EntityEditPart;
import UML_ER.diagram.edit.parts.EntityNameEditPart;
import UML_ER.diagram.edit.parts.GeneralizationEditPart;
import UML_ER.diagram.edit.parts.OntologyReference2EditPart;
import UML_ER.diagram.edit.parts.OntologyReferenceEditPart;
import UML_ER.diagram.edit.parts.OntologyReferenceRelationshipEditPart;
import UML_ER.diagram.edit.parts.RelationshipEditPart;
import UML_ER.diagram.edit.parts.RelationshipNameEditPart;
import UML_ER.diagram.edit.parts.RoleEditPart;
import UML_ER.diagram.edit.parts.RoleName2EditPart;
import UML_ER.diagram.edit.parts.RoleNameEditPart;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * 
 */
public class UML_ERVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "UML_ER.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (ERmodelEditPart.MODEL_ID.equals(view.getType())) {
				return ERmodelEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return UML_ER.diagram.part.UML_ERVisualIDRegistry.getVisualID(view
				.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				UML_ERDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return Integer.toString(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (UML_ERPackage.eINSTANCE.getERmodel().isSuperTypeOf(
				domainElement.eClass())
				&& isDiagram((ERmodel) domainElement)) {
			return ERmodelEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * 
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = UML_ER.diagram.part.UML_ERVisualIDRegistry
				.getModelID(containerView);
		if (!ERmodelEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (ERmodelEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = UML_ER.diagram.part.UML_ERVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = ERmodelEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case ERmodelEditPart.VISUAL_ID:
			if (UML_ERPackage.eINSTANCE.getEntity().isSuperTypeOf(
					domainElement.eClass())) {
				return EntityEditPart.VISUAL_ID;
			}
			if (UML_ERPackage.eINSTANCE.getRelationship().isSuperTypeOf(
					domainElement.eClass())
					&& !((Relationship) domainElement).getAttribute().isEmpty()) {
				return BinaryRelationshipEditPart.VISUAL_ID;
			}
			if (UML_ERPackage.eINSTANCE.getRelationship().isSuperTypeOf(
					domainElement.eClass())
					&& !UML_ERPackage.eINSTANCE.getBinaryRelationship()
							.isSuperTypeOf(domainElement.eClass())) {
				return RelationshipEditPart.VISUAL_ID;
			}
			break;
		case EntityAttributesEditPart.VISUAL_ID:
			if (UML_ERPackage.eINSTANCE.getAttribute().isSuperTypeOf(
					domainElement.eClass())) {
				return AttributeEditPart.VISUAL_ID;
			}
			break;
		case EntityAttributes2EditPart.VISUAL_ID:
			if (UML_ERPackage.eINSTANCE.getOntologyReference().isSuperTypeOf(
					domainElement.eClass())) {
				return OntologyReferenceEditPart.VISUAL_ID;
			}
			break;
		case BinaryRelationshipAttributesEditPart.VISUAL_ID:
			if (UML_ERPackage.eINSTANCE.getAttribute().isSuperTypeOf(
					domainElement.eClass())) {
				return AttributeEditPart.VISUAL_ID;
			}
			break;
		case BinaryRelationship2EditPart.VISUAL_ID:
			if (UML_ERPackage.eINSTANCE.getBinaryRelationship().isSuperTypeOf(
					domainElement.eClass())) {
				return AttributeEditPart.VISUAL_ID;
			}

			break;
		case AggregationEditPart.VISUAL_ID:
			if (UML_ERPackage.eINSTANCE.getBinaryRelationship().isSuperTypeOf(
					domainElement.eClass())) {
				return AttributeEditPart.VISUAL_ID;
			}
		case CompositionEditPart.VISUAL_ID:
			if (UML_ERPackage.eINSTANCE.getBinaryRelationship().isSuperTypeOf(
					domainElement.eClass())) {
				return AttributeEditPart.VISUAL_ID;
			}
		case RelationshipEditPart.VISUAL_ID:
			if (UML_ERPackage.eINSTANCE.getRelationship().isSuperTypeOf(
					domainElement.eClass())) {
				return AttributeEditPart.VISUAL_ID;
			}

			break;
		}
		return -1;
	}

	/**
	 *
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = UML_ER.diagram.part.UML_ERVisualIDRegistry
				.getModelID(containerView);
		if (!ERmodelEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (ERmodelEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = UML_ER.diagram.part.UML_ERVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = ERmodelEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case ERmodelEditPart.VISUAL_ID:
			if (EntityEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (BinaryRelationshipEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case EntityEditPart.VISUAL_ID:
			if (EntityNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (AttributeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (OntologyReferenceEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case BinaryRelationshipEditPart.VISUAL_ID:
			if (BinaryRelationshipAttributesEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case BinaryRelationshipAttributesEditPart.VISUAL_ID:
			if (Attribute2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case BinaryRelationship2EditPart.VISUAL_ID:
			if (BinaryRelationshipNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (BinaryRelationshipName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (BinaryRelationshipName3EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (BinaryRelationshipEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (AttributeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case RelationshipEditPart.VISUAL_ID:
			if (RelationshipNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (AttributeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case AggregationEditPart.VISUAL_ID:
			if (AttributeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case CompositionEditPart.VISUAL_ID:
			if (AttributeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * 
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}

		if (UML_ERPackage.eINSTANCE.getAggregation().isSuperTypeOf(
				domainElement.eClass())) {
			return AggregationEditPart.VISUAL_ID;
		}
		if (UML_ERPackage.eINSTANCE.getComposition().isSuperTypeOf(
				domainElement.eClass())) {
			return CompositionEditPart.VISUAL_ID;
		}
		if (UML_ERPackage.eINSTANCE.getGeneralization().isSuperTypeOf(
				domainElement.eClass())) {
			return GeneralizationEditPart.VISUAL_ID;
		}
		if (UML_ERPackage.eINSTANCE.getBinaryRelationship().isSuperTypeOf(
				domainElement.eClass())) {
			return BinaryRelationship2EditPart.VISUAL_ID;
		}
		if (UML_ERPackage.eINSTANCE.getRole().isSuperTypeOf(
				domainElement.eClass())
				&& !UML_ERPackage.eINSTANCE.getBinaryRelationship()
						.isSuperTypeOf(domainElement.eContainer().eClass())) {
			return RoleEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(ERmodel element) {
		return true;
	}

	/**
	 * @generated
	 */
	public static boolean checkNodeVisualID(View containerView,
			EObject domainElement, int candidate) {
		if (candidate == -1) {
			//unrecognized id is always bad
			return false;
		}
		int basic = getNodeVisualID(containerView, domainElement);
		return basic == candidate;
	}

	/**
	 * @generated
	 */
	public static boolean isCompartmentVisualID(int visualID) {
		switch (visualID) {
		case EntityAttributesEditPart.VISUAL_ID:
		case EntityAttributes2EditPart.VISUAL_ID:
		case BinaryRelationshipAttributesEditPart.VISUAL_ID:
			return true;
		default:
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static boolean isSemanticLeafVisualID(int visualID) {
		switch (visualID) {
		case ERmodelEditPart.VISUAL_ID:
			return false;
		case RelationshipEditPart.VISUAL_ID:
		case AttributeEditPart.VISUAL_ID:
		case OntologyReferenceEditPart.VISUAL_ID:
		case Attribute2EditPart.VISUAL_ID:
			return true;
		default:
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static final DiagramStructure TYPED_INSTANCE = new DiagramStructure() {
		/**
		 * @generated
		 */
		@Override
		public int getVisualID(View view) {
			return UML_ER.diagram.part.UML_ERVisualIDRegistry.getVisualID(view);
		}

		/**
		 * @generated
		 */
		@Override
		public String getModelID(View view) {
			return UML_ER.diagram.part.UML_ERVisualIDRegistry.getModelID(view);
		}

		/**
		 * @generated
		 */
		@Override
		public int getNodeVisualID(View containerView, EObject domainElement) {
			return UML_ER.diagram.part.UML_ERVisualIDRegistry.getNodeVisualID(
					containerView, domainElement);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean checkNodeVisualID(View containerView,
				EObject domainElement, int candidate) {
			return UML_ER.diagram.part.UML_ERVisualIDRegistry
					.checkNodeVisualID(containerView, domainElement, candidate);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean isCompartmentVisualID(int visualID) {
			return UML_ER.diagram.part.UML_ERVisualIDRegistry
					.isCompartmentVisualID(visualID);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean isSemanticLeafVisualID(int visualID) {
			return UML_ER.diagram.part.UML_ERVisualIDRegistry
					.isSemanticLeafVisualID(visualID);
		}
	};

}
